<template>
    <div class="edu-page-warp">
        <div v-if="indexKey === 1">
            <condition-card @reset="reset" @search="onSubmit" label-width="110px">
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item label="方案名称">
                            <el-input v-model="queryForm.actConfigName"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="方案状态">
                            <el-select v-model="queryForm.actConfigStatus" style="width: 100%;">
                                <el-option
                                    v-for="item in options.actConfigStatusList"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="当前年级">
                            <el-select
                                v-model="queryForm.currentGradeArr"
                                style="width: 100%;"
                                multiple
                                collapse-tags
                            >
                                <el-option
                                    v-for="item in options.years"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item :label="$t('label.trainingLevel')">
                            <el-select
                                v-model="queryForm.trainingLevelArr"
                                style="width: 100%;"
                                multiple
                                collapse-tags
                            >
                                <el-option
                                    v-for="item in options.trainingLevel"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('label.trainingCategory')">
                            <el-select v-model="queryForm.trainingCategoryArr" style="width: 100%;" multiple>
                                <el-option
                                    v-for="item in options.trainingCategory"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="激活方式">
                            <el-select v-model="queryForm.activationMethod" style="width: 100%;" >
                                <el-option
                                    v-for="item in options.activationMethod"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">
                    <el-col :span="8">
                        <el-form-item :label="$t('label.isOverseas')">
                            <el-select v-model="queryForm.isOverseasArr" style="width: 100%;" multiple>
                                <el-option
                                    v-for="item in options.isOverseas"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item :label="$t('label.formLearning')">
                            <el-select v-model="queryForm.formLearningArr" style="width: 100%;" multiple>
                                <el-option
                                    v-for="item in options.formLearning"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="入学季节">
                            <el-select v-model="queryForm.enrolSeasonArr" style="width: 100%;" multiple>
                                <el-option
                                    v-for="item in options.enrolSeason"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="24">
                    <el-col :span="8">
                            <el-form-item label="激活开始时间">
                                <el-row type="flex" justify="center" align="middle">
                                    <el-date-picker
                                        v-model="queryForm.queryStartTime"
                                        value-format="timestamp"
                                        type="datetime"
                                        placeholder="选择日期"
                                    ></el-date-picker>
                                    <span style="margin: 0 10px;">~</span>
                                    <el-date-picker
                                        v-model="queryForm.queryEndTime"
                                        type="datetime"
                                        value-format="timestamp"
                                        placeholder="选择日期"
                                    ></el-date-picker>
                                </el-row>
                            </el-form-item>
                        </el-col>
                </el-row>
            </condition-card>
            <el-card shadow="never">
                <div class="edu-page-title">激活说明设置</div>
                <div style="text-align:right">
                    <el-button @click="openFieldDialog" v-if="showSet">字段设置</el-button>
                    <el-button type="primary" @click="addActive" v-if="showAdd">{{$t('message.add')}}</el-button>
                </div>
                <edu-table
                    :data="activas"
                    size="mini"
                    style="width:100%;margin-top:20px"
                    :order="false"
                    @page-change="handleCurrentChange"
                    :total="queryForm.total_"
                    :pageSize="queryForm.pageSize_"
                    :current-page="queryForm.pageNum_"
                    @selection-change="selectionChange"
                >
                    <el-table-column
                        label="方案名称"
                        prop="actConfigName"
                        show-overflow-tooltip
                        width="120"
                    />
                    <el-table-column label="方案状态" show-overflow-tooltip>
                        <!-- <template slot-scope="scope">{{scope.row.actConfigStatus==1?"有效":"无效"}}</template> -->
                        <template slot-scope="scope">
                          <el-tag
                            type="info"
                            v-if="scope.row.actConfigStatus == 0"
                          >无效</el-tag>
                          <el-tag
                            type="success"
                            v-else-if="scope.row.actConfigStatus == 1"
                          >有效</el-tag>
                        </template>
                    </el-table-column>
                     <el-table-column label="字段设置" show-overflow-tooltip>
                        <!-- <template slot-scope="scope">{{scope.row.actConfigStatus==1?"有效":"无效"}}</template> -->
                        <template slot-scope="scope">
                          <el-tag
                            type="success"
                            v-if="scope.row.configured"
                          >已配置</el-tag>
                          <el-tag
                            type="info"
                            v-else
                          >未配置</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="当前年级"
                        prop="currentGradeJoin"
                        show-overflow-tooltip
                        width="140"
                    />
                    <el-table-column
                        :label="$t('label.trainingLevel')"
                        show-overflow-tooltip
                        width="110"
                    >
                    <template slot-scope="scope">{{contactListLabel(scope.row.trainingLevelArr, 'trainingLevelI18n')}}</template>
                    </el-table-column>
                    <el-table-column :label="$t('label.trainingCategory')" show-overflow-tooltip width="180">
                    <template slot-scope="scope">{{contactListLabel(scope.row.trainingCategoryArr, 'trainingCategoryI18n')}}</template>
                    </el-table-column>
                    <el-table-column :label="$t('label.isOverseas')" width="120" show-overflow-tooltip>
                        <template slot-scope="scope">{{notOrYes(scope.row.isOverseasArr, 'isOverseas')}}</template>
                    </el-table-column>
                    <el-table-column
                        :label="$t('label.formLearning')"
                        show-overflow-tooltip
                        width="110"
                    >
                    <template slot-scope="scope">{{contactListLabel(scope.row.formLearningArr, 'formLearningI18n')}}</template>
                    </el-table-column>
                    <el-table-column label="入学季节" show-overflow-tooltip>
                        <template slot-scope="scope">{{contactListLabel(scope.row.enrolSeasonArr, 'enrolSeasonI18n')}}</template>
                    </el-table-column>
                    <el-table-column label="激活说明" prop="remark" show-overflow-tooltip/>
                    <el-table-column label="激活方式" show-overflow-tooltip>
                        <template slot-scope="scope">{{scope.row.activationMethod==1?"按频次":"按时间"}}</template>
                    </el-table-column>
                    <el-table-column label="频次" show-overflow-tooltip>
                        <template slot-scope="scope">{{getFrequencyTextByValue(scope.row.frequency)}}</template>
                    </el-table-column>
                    <el-table-column label="激活开始时间" width="150" show-overflow-tooltip>
                        <template slot-scope="scope">{{getDateFormat(scope.row.activationTime)}}</template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button
                                @click="ActivationEdit(scope, 'edit')"
                                type="text"
                                v-if="showEdit"
                            >{{$t('message.edit')}}</el-button>
                            <el-button
                                @click="ActivationDelete(scope)"
                                type="text"
                                v-if="showDelete"
                            >{{$t('message.delete')}}</el-button>
                            <el-button 
                            @click="ActivationEdit(scope, 'copy')"
                            type="text"
                            >复制</el-button>
                            <el-button 
                            @click="toStudentList(scope.row)"
                            type="text"
                            >学生名单</el-button>
                        </template>
                    </el-table-column>
                </edu-table>
            </el-card>
            <edu-dialog
                title="信息激活维护"
                width="50%"
                ref="ActiveDialog"
                :autoHeight="true"
                @close="closeActive"
                @confirm="saveActive"
            >
                <el-form
                    label-width="110px"
                    label-suffix=":"
                    :model="activeForm"
                    ref="ActiveForm"
                    :rules="rules"
                >
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="激活方案名称" prop="actConfigName">
                                <el-input
                                    v-model="activeForm.actConfigName"
                                    maxlength="50"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="方案状态" prop="actConfigStatus">
                                <el-select v-model="activeForm.actConfigStatus" style="width: 100%;" >
                                    <el-option
                                        v-for="item in options.actConfigStatusList"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="当前年级" prop="currentGradeArr">
                                <el-select v-model="activeForm.currentGradeArr" style="width:100%;" multiple>
                                    <el-option v-for="item in options.years"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item :label="$t('label.trainingLevel')" prop="trainingLevelArr">
                                <el-select
                                    v-model="activeForm.trainingLevelArr"
                                    style="width: 100%;"
                                    multiple
                                    collapse-tags
                                >
                                    <el-option
                                        v-for="item in options.trainingLevel"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item :label="$t('label.trainingCategory')" prop="trainingCategoryArr">
                                <el-select v-model="activeForm.trainingCategoryArr" style="width: 100%;" multiple>
                                    <el-option
                                        v-for="item in options.trainingCategory"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item :label="$t('label.isOverseas')" prop="isOverseasArr">
                                <el-select v-model="activeForm.isOverseasArr" style="width: 100%;" multiple>
                                    <el-option
                                        v-for="item in options.isOverseas"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item :label="$t('label.formLearning')" prop="formLearningArr">
                                <el-select v-model="activeForm.formLearningArr" style="width: 100%;" multiple>
                                    <el-option
                                        v-for="item in options.formLearning"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="入学季节" prop="enrolSeasonArr">
                                <el-select v-model="activeForm.enrolSeasonArr" style="width: 100%;" multiple>
                                    <el-option
                                        v-for="item in options.enrolSeason"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="允许关闭" prop="isClose">
                                <el-select v-model="activeForm.isClose" style="width: 100%;">
                                    <el-option
                                        v-for="item in options.isClose"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <div class="active-type">
                        <span
                            :class="activeForm.activationMethod==1?'active':null"
                            @click="changeActive(1)"
                        >按频次</span>
                        <span class="split">|</span>
                        <span
                            :class="activeForm.activationMethod==2?'active':null"
                            @click="changeActive(2)"
                        >按时间</span>
                    </div>
                    <div v-if="activeForm.activationMethod==1">
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <el-form-item label="频次" prop="frequency" key="k1">
                                    <el-select v-model="activeForm.frequency" style="width: 100%;">
                                        <el-option
                                            v-for="item in options.frequencys"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="激活说明" prop="remark" key="k2">
                                    <el-input
                                        type="textarea"
                                        v-model="activeForm.remark"
                                        maxlength="255"
                                        rows="4"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                    <div v-else>
                        <el-row :gutter="20">
                            <el-col :span="12">
                                <el-form-item label="开始时间" prop="activationTime" key="k3">
                                    <el-date-picker
                                        type="datetime"
                                        v-model="activeForm.activationTime"
                                        style="width:100%"
                                        value-format="timestamp"
                                    ></el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="20">
                            <el-col :span="24">
                                <el-form-item label="激活说明" prop="remark" key="k4">
                                    <el-input
                                        type="textarea"
                                        v-model="activeForm.remark"
                                        maxlength="255"
                                        rows="4"
                                    ></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
                <template slot="buttons">
                    <el-button @click="returnActive">{{$t('message.return')}}</el-button>
                    <el-button @click="saveActive" type="primary">{{$t('message.save')}}</el-button>
                </template>
            </edu-dialog>
            <edu-dialog
                title="字段权限设置"
                width="50%"
                ref="fieldDialog"
                :autoHeight="true"
                @close="closeField"
                @confirm="saveField"
            >
                <edu-table
                    :data="fields"
                    size="mini"
                    style="width:100%;"
                    :selection="false"
                    :pagination="false"
                >
                    <el-table-column label="字段分类">
                        <template slot-scope="scope">
                            <el-select
                                :id="scope.row.keyId"
                                style="width:100%"
                                v-model="scope.row.fieldClaas"
                                @change="changeFieldClaas(scope)"
                            >
                                <el-option
                                    v-for="item in fieldTypes"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="字段名" width="280px">
                        <template slot-scope="scope">
                            <el-select
                                :id="scope.row.keyId"
                                style="width:100%"
                                v-model="scope.row.fieldId"
                            >
                                <el-option
                                    v-for="item in scope.row.setFields"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                />
                            </el-select>
                        </template>
                    </el-table-column>
                    <el-table-column label="允许编辑">
                        <template slot-scope="scope">
                            <el-checkbox :id="scope.row.keyId" v-model="scope.row.isUpdate"></el-checkbox>
                        </template>
                    </el-table-column>
                    <el-table-column label="是否必填">
                        <template slot-scope="scope">
                            <el-checkbox :id="scope.row.keyId" v-model="scope.row.isRequired"></el-checkbox>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button type="text" @click="addRow">新增行</el-button>
                            <el-button type="text" @click="deleteRow(scope)">删除行</el-button>
                        </template>
                    </el-table-column>
                </edu-table>
            </edu-dialog>
        </div>
        <div v-if="indexKey === 2">
            <student-list
            :configId="configId"
            @returnList="returnList"
            />
        </div>
    </div>
    

</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import studentList from './studentList.vue'
import moment from "moment";
import {
    addConfig,
    configList,
    getConfig,
    updateConfig,
    deleteConfig,
    fieldType,
    fieldsByfieldType,
    fieldConfig,
    singleConfig
} from "common/src/api/studentinfo";
export default {
    components: {
        'student-list': studentList
    },
    name: "infoActivationSetting",
    data() {
        return {
            queryForm: {
                actConfigName: "", // 方案名称，
                isOverseasArr: [],
                formLearningArr: [],
                activationMethod: null,
                queryStartTime: null,
                queryEndTime: null,
                actConfigStatus: null,
                currentGradeArr: [],
                trainingLevelArr: [],
                trainingCategoryArr: [],
                enrolSeasonArr: [],
                pageSize_: 20,
                pageNum_: 1,
                total_: 0
            },
            configId: null,
            indexKey: 1,
            activeForm: {
                trainingLevelArr: [], // 培养层次
                isOverseasArr: [], // 是否国际生
                trainingCategoryArr: [],
                frequency: null, //激活频次
                activationTime: null, //激活时间
                remark: null, //激活说明
                activationMethod: 1, //激活方式
                isClose: null,
                enrolSeasonArr: [],
                formLearningArr: [], // 学习形式
                currentGradeArr: [], // 当前年级
                actConfigName: "", // 方案名称，
                actConfigStatus: null // 方案状态
            },
            options: {
                trainingLevel: [],
                trainingCategory: [],
                isOverseas: [],
                formLearning: [],
                activationMethod: [],
                frequencys: [],
                isClose: [],
                actConfigStatusList: [
                    {
                        label: "有效",
                        value: 1
                    },
                    {
                        label: "无效",
                        value: 0
                    }
                ],
                years: [],
                enrolSeason: []
            },
            fieldTypes: [], //字段分类
            activas: [], //激活信息列表
            fields: [], //字段列表
            selectedActives: [], //选中的激活列表
            editId: null,
            showAdd: false,
            showEdit: false,
            showDelete: false,
            showSet: false,
            rules: {
                actConfigName: [
                    {
                        required: true,
                        message: "请输入方案名称",
                        trigger: "blur"
                    }
                ],
                actConfigStatus: [
                    {
                        required: true,
                        message: "请选择方案状态",
                        trigger: "change"
                    }
                ],
                currentGradeArr: [
                    {
                        required: true,
                        message: "请选择当前年级",
                        trigger: "change"
                    }
                ],
                isClose: [
                    {
                        required: true,
                        message: "请选择是否允许关闭",
                        trigger: "change"
                    }
                ],
                frequency: [
                    {
                        required: true,
                        message: "请选择频次",
                        trigger: "change"
                    }
                ],
                remark: [
                    {
                        required: true,
                        message: "请输入激活说明",
                        trigger: "blur"
                    }
                ],
            }
        };
    },
    mounted() {
        this.init();
        let session = localStorage.getItem("sessiondata");
        session = JSON.parse(session);
        let user = session.mockUser || session.user;
        // if (user.uid == "admin1") {
        //     return;
        // }
        if (user.innerRoles.includes("1")) {
            this.showSet = true;
            this.showAdd = true;
            this.showEdit = true;
            this.showDelete = true;
        }
    },
    methods: {
        init() {
            this.options.activationMethod.push({
                label: "按频次",
                value: 1
            });
            this.options.activationMethod.push({
                label: "按时间",
                value: 2
            });
            this.options.frequencys.push({
                label: "首次",
                value: 1
            });
            this.options.frequencys.push({
                label: "每月",
                value: 2
            });
            this.options.frequencys.push({
                label: "每学期",
                value: 3
            });
            this.options.frequencys.push({
                label: "每学年",
                value: 4
            });
            this.options.isClose.push({
                label: "是",
                value: 1
            });
            this.options.isClose.push({
                label: "否",
                value: 0
            });
            let currnetYear = new Date().getFullYear() + 2;
            for (let index = 17; index >= 0; index--) {
            let year = currnetYear - index;
            this.options.years.push({ label: year, value: year });
            }
            let data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: [
                    "X_RXJJ",
                    "X_PYCC",
                    "X_PYLB",
                    "isAborad",
                    "X_XWLX",
                    "X_XXXS",
                    "X_RXFSN",
                    "enrolType"
                ]
            };
            queryDic(data).then(res => {
                if (res.code == 200) {
                    UTILS.fillSelect(
                        {
                            trainingLevel: "X_PYCC",
                            formLearning: "X_XXXS",
                            trainingCategory: "X_PYLB",
                            isOverseas: "isAborad",
                            enrolSeason:"X_RXJJ"
                        },
                        res.data,
                        this.options
                    );
                }
            });
            fieldType().then(res => {
                if (res.code == 200) {
                    for (let key in res.data) {
                        this.fieldTypes.push({
                            label: res.data[key].name,
                            value: res.data[key].nameEn
                        });
                    }
                }
            });

            this.onSubmit();
        },
        // 学生名单
        toStudentList(row) {
            this.configId = row.id
            this.indexKey = 2
        },
        // 学生名单页返回
        returnList() {
            this.indexKey = 1
            this.configId = null
        },
        reset() {
            this.queryForm.actConfigName = "";
            this.queryForm.actConfigStatus = null;
            this.queryForm.currentGradeArr = [];
            this.queryForm.trainingLevelArr = [];
            this.queryForm.trainingCategoryArr = [];
            this.queryForm.isOverseasArr = [];
            this.queryForm.formLearningArr = [];
            this.queryForm.enrolSeasonArr = [];
            this.queryForm.activationMethod = null;
            this.queryForm.queryStartTime = null;
            this.queryForm.queryEndTime = null;
            this.onSubmit()
            
        },
        onSubmit() {
            let postData = _.cloneDeep(this.queryForm)
            postData.currentGradeArr = this.changeData(postData.currentGradeArr,"currentGrade")
            postData.trainingLevelArr = this.changeData(postData.trainingLevelArr,"trainingLevel") 
            postData.trainingCategoryArr = this.changeData(postData.trainingCategoryArr,"trainingCategory") 
            postData.isOverseasArr = this.changeData(postData.isOverseasArr,"isOverseas") 
            postData.formLearningArr = this.changeData(postData.formLearningArr,"formLearning") 
            postData.enrolSeasonArr = this.changeData(postData.enrolSeasonArr,"enrolSeason") 
            postData.activationMethod = postData.activationMethod ? Number(postData.activationMethod) : null
            configList(postData).then(res => {
                if (res.code == 200) {
                    this.activas = res.data.list;
                    this.queryForm.total_ = res.data.total_;
                }
            });
        },
        handleCurrentChange(val) {
            this.queryForm.pageSize_ = val.pageSize;
            this.queryForm.pageNum_ = val.pageNum;
            this.onSubmit();
        },
        ActivationEdit(scope, type) {
            getConfig(scope.row.id).then(res => {
                if (res.code == 200) {
                    if (this.$refs.ActiveForm)
                        this.$refs.ActiveForm.clearValidate();
                        let dataStrArr = []
                        if(type === "edit") {
                            this.editId = res.data.id;
                            this.activeForm.actConfigName = res.data.actConfigName;
                        } else if(type === "copy") {
                            this.editId = null
                            this.activeForm.fieldConfigs = res.data.fieldConfigs;
                            this.activeForm.actConfigName = ""
                        }
                    
                    this.activeForm.remark = res.data.remark;
                    dataStrArr = res.data.currentGradeJoin ? res.data.currentGradeJoin.split(",") : [];
                    if(dataStrArr && dataStrArr.length > 0) {
                        this.activeForm.currentGradeArr=dataStrArr.map(item => {  
                        return +item;  
                    });
                    }
                    this.activeForm.activationMethod = res.data.activationMethod;
                    this.activeForm.isOverseasArr = res.data.isOverseasJoin ? res.data.isOverseasJoin.split(",") : [];
                    this.activeForm.frequency = res.data.frequency;
                    this.activeForm.actConfigStatus = res.data.actConfigStatus;
                    this.activeForm.activationTime = res.data.activationTime;
                    this.activeForm.trainingCategoryArr = res.data.trainingCategoryJoin ? res.data.trainingCategoryJoin.split(",") : [];
                    this.activeForm.formLearningArr = res.data.formLearningJoin ? res.data.formLearningJoin.split(",") : [];
                    this.activeForm.trainingLevelArr = res.data.trainingLevelJoin ? res.data.trainingLevelJoin.split(",") : [];
                    this.activeForm.isClose = res.data.isClose;
                    this.activeForm.enrolSeasonArr = res.data.enrolSeasonJoin ? res.data.enrolSeasonJoin.split(",") : [];
                    this.$refs.ActiveDialog.open();
                }
            });
        },
        //删除激活信息
        ActivationDelete(scope) {
            this.$confirm(
                this.$t("message.deleteConfirm"),
                this.$t("message.prompt"),
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning"
                }
            ).then(() => {
                deleteConfig(scope.row.id).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            message: this.$t("message.deleteSuccess"),
                            type: "success"
                        });
                        this.onSubmit();
                    }
                });
            });
        },
        resetActiveForm() {
            this.activeForm.activationMethod = 1;
            this.activeForm.trainingLevelArr = [];
            this.activeForm.actConfigName = ""
            this.activeForm.actConfigStatus = null
            this.activeForm.currentGradeArr = []
            this.activeForm.trainingCategoryArr = [];
            this.activeForm.isOverseasArr = [];
            this.activeForm.formLearningArr = [];
            this.activeForm.enrolSeasonArr = null;
            this.activeForm.isClose = null;
            this.activeForm.frequency = null;
            this.activeForm.activationTime = null;
            this.activeForm.remark = null;
        },
        //添加激活
        addActive() {
            this.editId = null;
            this.resetActiveForm()
            this.$refs.ActiveDialog.open();
            if (this.$refs.ActiveForm) this.$refs.ActiveForm.clearValidate();
        },
        //激活信息表单是否有值
        activeFormHasValue() {
            return (
                this.activeForm.trainingLevelArr.length != 0 ||
                this.activeForm.trainingCategoryArr.length != 0 ||
                this.activeForm.isOverseasArr.length != 0 ||
                this.activeForm.formLearningArr.length != 0 ||
                this.activeForm.enrolSeasonArr != null ||
                this.activeForm.isClose != null ||
                this.activeForm.frequency != null ||
                this.activeForm.activationTime != null ||
                this.activeForm.remark != null
            );
        },
        showActiveTipDialog() {
            this.$confirm("返回将不会保存当前信息激活设置，是否确定?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                    this.resetActiveForm();
                    this.$refs.ActiveDialog.close(); 
                }).catch(() => {});
        },
        // 返回激活对话框
        returnActive() {
            if (this.editId) {
                this.$refs.ActiveDialog.close();
            } else {
                if (this.activeFormHasValue()) {
                    this.showActiveTipDialog();
                } else {
                    this.$refs.ActiveDialog.close();
                }
            }
        },
        // 关闭激活对话框
        closeActive() {
            if (this.editId) {
                this.resetActiveForm();
            } else {
                if (this.activeFormHasValue()) {
                    this.$refs.ActiveDialog.open();
                    this.showActiveTipDialog();
                }
            }
        },
        // 保存激活对话框
        saveActive() {
            this.$refs.ActiveForm.validate(valid => {
                if (valid) {
                    let fn = res => {
                        if (res.code == 200) {
                            this.$message({
                                message: "提交成功",
                                type: "success"
                            });
                            this.resetActiveForm();
                            this.$refs.ActiveDialog.close();
                            this.onSubmit();
                        }
                    };
                    let postData = _.cloneDeep(this.activeForm);
                    postData.currentGradeArr = this.changeData(postData.currentGradeArr,"currentGrade")
                    postData.trainingLevelArr = this.changeData(postData.trainingLevelArr,"trainingLevel") 
                    postData.trainingCategoryArr = this.changeData(postData.trainingCategoryArr,"trainingCategory") 
                    postData.isOverseasArr = this.changeData(postData.isOverseasArr,"isOverseas") 
                    postData.formLearningArr = this.changeData(postData.formLearningArr,"formLearning") 
                    postData.enrolSeasonArr = this.changeData(postData.enrolSeasonArr,"enrolSeason") 
                    postData.activationMethod = postData.activationMethod ? Number(postData.activationMethod) : null
                    if (this.editId) {
                        postData.id = this.editId;
                        updateConfig(postData).then(fn);
                    } else {
                        addConfig(postData).then(fn);
                    }
                }
            });
        },
        // 转换数据
        changeData(list, labelName) {
            if (list && list.length > 0 && labelName) {
                // let currentObj = {}
                // return list.map(val => {
                //     currentObj[labelName] = val
                //     return currentObj
                // })
                let arrList = []
                list.forEach(val => {
                    let currentObj = {}
                    currentObj[labelName] = val
                    arrList.push(currentObj)
                })
                return arrList
            } else {
                return null
            }
        },
        //改变激活类型
        changeActive(val) {
            if (this.activeForm.activationMethod != val) {
                this.activeForm.activationMethod = val;
                this.activeForm.remark = null;
                this.activeForm.frequency = null;
                this.activeForm.activationTime = null;
            }
        },
        //选中项改变
        selectionChange(val) {
            this.selectedActives = val;
        },
        openFieldDialog() {
            if (this.selectedActives.length == 0) {
                this.$message({
                    message: "请选中要设置的项"
                });
                return;
            }
            this.getFields();
        },
        //获取选中项对应字段列表
        getFields() {
            //选中单项，查询对应字段列表;选中多个，不查询给默认项
            if (this.selectedActives.length === 1) {
                singleConfig({ configId: this.selectedActives[0].id }).then(
                    res => {
                        if (res.code == 200) {
                            this.$refs.fieldDialog.open();
                            if (res.data.length == 0) {
                                this.fields = [
                                    {
                                        keyId: 0,
                                        fieldId: null,
                                        fieldClaas: null,
                                        isUpdate: false,
                                        isRequired: false,
                                        setFields: []
                                    }
                                ];
                            } else {
                                let fields = res.data.map((c, index) => {
                                    return {
                                        keyId: index,
                                        fieldId: c.fieldId,
                                        fieldClaas: null,
                                        isUpdate: c.isUpdate ? true : false,
                                        isRequired: c.isRequired ? true : false,
                                        setFields: [],
                                        fieldSingleClass: c.fieldClaas
                                    };
                                });
                                let clazz = [];
                                fields.forEach(c => {
                                    let obj = this.fieldTypes.find(
                                        t =>
                                            t.value.indexOf(
                                                c.fieldSingleClass
                                            ) != -1
                                    );
                                    if (obj) {
                                        c.fieldClaas = obj.value;
                                    }
                                    if (!clazz.includes(c.fieldSingleClass)) {
                                        clazz.push(c.fieldSingleClass);
                                    }
                                    // if (!clazz.includes(c.fieldClaas)) {
                                    //     clazz.push(c.fieldClaas);
                                    // }
                                });
                                //查询类的所有字段集合
                                fieldsByfieldType(clazz.join(",")).then(res => {
                                    if (res.code == 200) {
                                        let map = new Map();
                                        clazz.forEach(clz => {
                                            let options = res.data.filter(
                                                c => c.tableStr == clz
                                            );
                                            if (options.length > 0) {
                                                map.set(
                                                    clz,
                                                    options.map(c => {
                                                        return {
                                                            label: c.name,
                                                            value: c.id,
                                                            className:
                                                                c.tableStr
                                                        };
                                                    })
                                                );
                                            }
                                        });
                                        //设置字段集合引用
                                        fields.forEach(field => {
                                            if (
                                                map.has(field.fieldSingleClass)
                                            ) {
                                                field.setFields = map.get(
                                                    field.fieldSingleClass
                                                );
                                            }
                                        });
                                    }
                                });
                                this.fields = fields;
                            }
                        }
                    }
                );
            } else {
                this.$refs.fieldDialog.open();
                this.fields = [
                    {
                        keyId: 0,
                        fieldId: null,
                        fieldClaas: null,
                        isUpdate: false,
                        isRequired: false,
                        setFields: []
                    }
                ];
            }
        },
        addRow() {
            let maxObj = _.maxBy(this.fields, field => field.keyId);
            this.fields.push({
                keyId: maxObj.keyId + 1,
                fieldId: null,
                fieldClaas: null,
                isUpdate: false,
                isRequired: false,
                setFields: []
            });
        },
        deleteRow(scope) {
            if (this.fields.length == 1) return;
            this.fields = _.remove(
                this.fields,
                field => field.keyId != scope.row.keyId
            );
        },
        //关闭字段设置对话框
        closeField() {
            this.fields = [];
        },
        //保存字段设置
        saveField() {
            let nullOption = this.fields.find(
                field => field.fieldId == null || field.fieldClaas == null
            );
            if (nullOption) {
                this.$message({
                    message: "字段分类和字段名必须选择",
                    type: "warning"
                });
            } else {
                let postData = {
                    configIds: this.selectedActives.map(c => c.id),
                    fieldConfigs: this.fields.map(c => {
                        let selectOption = c.setFields.find(
                            c_ => c_.value == c.fieldId
                        );
                        return {
                            fieldId: c.fieldId,
                            fieldClaas: selectOption.className,
                            isUpdate: c.isUpdate ? 1 : 0,
                            isRequired: c.isRequired ? 1 : 0
                        };
                    })
                };
                fieldConfig(postData).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            message: this.$t("message.saveSuccess"),
                            type: "success"
                        });
                        this.$refs.fieldDialog.close();
                    }
                });
            }
        },
        //根据行的字段分类查询字段列表
        changeFieldClaas(scope) {
            let param = scope.row.fieldClaas;
            fieldsByfieldType(param).then(res => {
                if (res.code == 200) {
                    scope.row.fieldId = null;
                    scope.row.setFields = res.data.map(field => {
                        return {
                            label: field.name,
                            value: field.id,
                            className: field.tableStr
                        };
                    });
                }
            });
        },
        //获取格式化日期
        getDateFormat(val) {
            return val ? moment(val).format("YYYY-MM-DD HH:mm:ss") : null;
        },
        //根据值获取频次文本值
        getFrequencyTextByValue(value) {
            let option = this.options.frequencys.find(
                option => option.value == value
            );
            return option ? option.label : null;
        },
        //根据值获取是否留学生文本值
        getIsOverseasTextByValue(value) {
            let option = this.options.isOverseas.find(
                option => option.value == value
            );
            return option ? option.label : null;
        },
        contactListLabel(list, labelName) {
            let arrList = []
            if(list && list.length > 0) {
                arrList = list.map(obj => {
                    return obj[labelName]
                })
            } else {
                arrList = ["全部"]
            }    
            return arrList.join(',')
        },
        notOrYes(list, labelName) {
            let arrList = []
            if(list && list.length > 0) {
                arrList = list.map(obj => {
                    if(obj[labelName] == '0') {
                        return '否'
                    } else if(obj[labelName] == '1') {
                        return '是'
                    }
                    
                })
            } else {
                arrList = ["全部"]
            }
            return arrList.join(',')

        }

    }
};
</script>
<style lang="scss" scoped>
.active-type {
    font-size: 16px;
    cursor: pointer;
    margin-bottom: 20px;
}
.split {
    padding: 0 10px;
    font-size: 22px;
}
.active {
    color: #3a7fe8;
}
</style>
